const express = require('express');
const router = express.Router();
const { sendResponse } = require('@/utils/tool')
const { createRole, getRoleById, getAllRoles, getPaginatedRoles, updataRole, deleteRole } = require("@/services/role.services")


/**
 * @swagger
 * /createRole:
 *   post:
 *     summary: 新增角色
 *     description: 新增角色
 *     tags:
 *       - 角色管理
 *     security:
 *       - authorization: []
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *                 description: 角色名称
 *               description:
 *                 type: string
 *                 description: 角色描述
 *     responses:
 *       200:
 *         description: 请求成功
 *       400:
 *         $ref: '#/components/responses/paramsBad'
 *       401:
 *         $ref: '#/components/responses/unauthorized'
 *       500:
 *         $ref: '#/components/responses/error'
 */
router.post('/createRole', async (req, res) => {
  let { name, description } = req.body
  let result = await createRole({ name, description })
  sendResponse(req, res, result)
})

/**
 * @swagger
 * /getRoleById/{id}:
 *   get:
 *     summary: 获取角色信息
 *     description: 通过角色ID获取角色信息
 *     tags:
 *       - 角色管理
 *     security:
 *       - authorization: []
 *     parameters:
 *       - 
 *         name: id
 *         in: path
 *         description: 角色id
 *         required: true
 *     responses:
 *       200:
 *         description: 请求成功
 *       400:
 *         $ref: '#/components/responses/paramsBad'
 *       401:
 *         $ref: '#/components/responses/unauthorized'
 *       500:
 *         $ref: '#/components/responses/error'
 */
router.get('/getRoleById/:id', async (req, res) => {
  let { id } = req.params
  let result = await getRoleById(id)
  sendResponse(req, res, result)
})

/**
 * @swagger
 * /getAllRoles:
 *   get:
 *     summary: 获取所有角色信息
 *     description: 获取所有角色信息
 *     tags:
 *       - 角色管理
 *     security:
 *       - authorization: []
 *     responses:
 *       200:
 *         description: 请求成功
 *       400:
 *         $ref: '#/components/responses/paramsBad'
 *       401:
 *         $ref: '#/components/responses/unauthorized'
 *       500:
 *         $ref: '#/components/responses/error'
 */
router.get('/getAllRoles', async (req, res) => {
  let { order } = req.query
  let result = await getAllRoles(order)
  sendResponse(req, res, result)
})

/**
 * @swagger
 * /getPaginatedRoles:
 *   get:
 *     summary: 分页获取角色列表
 *     description: 分页获取角色列表
 *     tags:
 *       - 角色管理
 *     security:
 *       - authorization: []
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               page:
 *                 type: number
 *                 description: 页码
 *               pageSize:
 *                 type: number
 *                 description: 数量
 *               order:
 *                 type: string
 *                 description: 排序字段 ASC/DESC
 *     responses:
 *       200:
 *         description: 请求成功
 *       400:
 *         $ref: '#/components/responses/paramsBad'
 *       401:
 *         $ref: '#/components/responses/unauthorized'
 *       500:
 *         $ref: '#/components/responses/error'
 */
router.get('/getPaginatedRoles', async (req, res) => {
  let { page, pageSize, order } = req.query
  console.log('page, pageSize, order :>> ', page, pageSize, order);
  let result = await getPaginatedRoles(page, pageSize, order)
  sendResponse(req, res, result)
})

/**
 * @swagger
 * /updataRole:
 *   put:
 *     summary: 更新角色信息
 *     description: 更新角色信息
 *     tags:
 *       - 角色管理
 *     security:
 *       - authorization: []
 *     requestBody:
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               id:
 *                 type: string
 *                 description: 角色id
 *               name:
 *                 type: string
 *                 description: 角色名称
 *               description:
 *                 type: string
 *                 description: 角色描述
 *     responses:
 *       200:
 *         description: 请求成功
 *       400:
 *         $ref: '#/components/responses/paramsBad'
 *       401:
 *         $ref: '#/components/responses/unauthorized'
 *       500:
 *         $ref: '#/components/responses/error'
 */
router.put('/updataRole', async (req, res) => {
  let { id, name, description } = req.body
  let updates = { name, description }
  let result = await updataRole(id, updates)
  sendResponse(req, res, result)
})

/**
 * @swagger
 * /deleteRole/{id}:
 *   delete:
 *     summary: 删除角色
 *     description: 删除角色
 *     tags:
 *       - 角色管理
 *     security:
 *       - authorization: []
 *     parameters:
 *       - id: id
 *         in: path
 *         description: 角色id
 *     responses:
 *       200:
 *         description: 请求成功
 *       400:
 *         $ref: '#/components/responses/paramsBad'
 *       401:
 *         $ref: '#/components/responses/unauthorized'
 *       500:
 *         $ref: '#/components/responses/error'
 */
router.delete('/deleteRole/:id', async (req, res) => {
  let { id } = req.params
  let result = await deleteRole(id)
  sendResponse(req, res, result)
})

module.exports = router